package switchviz.gui.component;

import java.awt.Dimension;
import java.io.IOException;
import java.util.Iterator;

import org.jdom.JDOMException;

import switchviz.core.alias.AliasDataModel;
import switchviz.core.alias.AliasTree;
import switchviz.core.view.ViewDataModel;
import switchviz.core.view.ViewTree;
import switchviz.network.netswitch.SwitchDataModel;
import switchviz.network.netswitch.SwitchTree;
import switchviz.recording.RecordDataModel;
import switchviz.recording.RecordPersistence;


public class JXMLComboBox extends JLabeldComponent
{

	/**
	 * 
	 */
	public static final int SWITCHLIST = 0;
	public static final int VIEWLIST = 1;
	public static final int ALIASLIST = 2;
	public static final int RECORDLIST = 3;
	private static final long serialVersionUID = -1978528442981499426L;
	private int _type;
	
	public JXMLComboBox(int type) throws JDOMException, IOException
	{
		this("", type);
	}
	
	public JXMLComboBox(String labeltext, int type, Dimension dimension) throws JDOMException, IOException
	{
		super(labeltext, JLabeldComponent.COMBOBOX, dimension);
		_type = type;
		loadList();
	}
	
	public JXMLComboBox(String labeltext, int type) throws JDOMException, IOException
	{
		super(labeltext, JLabeldComponent.COMBOBOX);
		_type = type;
		loadList();
	}
	
	public void reload() throws JDOMException, IOException
	{
		this.getComboBox().removeAllItems();
		loadList();
	}
	
	public void addItem(Object obj)
	{
		switch(_type)
		{
		case SWITCHLIST:
			this.getComboBox().addItem((SwitchDataModel) obj);
			this.getComboBox().setSelectedItem((SwitchDataModel) obj);
			break;
		case VIEWLIST:
			this.getComboBox().addItem((ViewDataModel) obj);
			this.getComboBox().setSelectedItem((ViewDataModel) obj);
			break;
		case ALIASLIST:
			this.getComboBox().addItem((AliasDataModel) obj);
			this.getComboBox().setSelectedItem((AliasDataModel) obj);
			break;	
		case RECORDLIST:
			this.getComboBox().addItem((RecordDataModel) obj);
			this.getComboBox().setSelectedItem((RecordDataModel) obj);
			break;
		}
	}
	
	private void loadList() throws JDOMException, IOException
	{
		switch(_type)
		{
		case SWITCHLIST:
			Iterator<SwitchDataModel> switchiter = SwitchTree.getSwitchList().iterator();
			while(switchiter.hasNext())
			{
				this.getComboBox().addItem(switchiter.next());	
			}
			break;
		case VIEWLIST:
			Iterator<ViewDataModel> viewiter = ViewTree.getViewList().iterator();
			while(viewiter.hasNext())
			{
				this.getComboBox().addItem(viewiter.next());
			}
			break;
		case ALIASLIST:
			Iterator<AliasDataModel> aliasiter = AliasTree.getAliasList().iterator();
			while(aliasiter.hasNext())
			{
				this.getComboBox().addItem(aliasiter.next());
			}
			break;
		case RECORDLIST:
			Iterator<RecordDataModel> recorditer = RecordPersistence.getInstance().getRecordList().iterator();
			while(recorditer.hasNext())
			{
				this.getComboBox().addItem(recorditer.next());
			}
			break;
		}
	}
	
	
}
